// jdbc 工具类

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.*;
import java.util.Properties;

public class JDBCUtiles {
    public static final String className ;
    public static final String url ;
    public static final String user ;
    public static final String password ;

    static {
        Properties properties = new Properties();   //
        try {
            properties.load(new FileInputStream("src/file/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
//        最终类型，值不能改变，如果上面赋值了则这里报错
//        此处赋值是为了第一次加载配置信息到文件中
        className = "com.mysql.cj.jdbc.Driver";
        url = "jdbc:mysql://192.168.56.101:3306/test";
        user = "root";
        password = "1234";

        className = properties.getProperty("className");
        url = properties.getProperty("url");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
    }

//    注册驱动
    public static void loadDriver() {
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

//    建立SQL语句对象
    public static void stateMent(Connection connection) {
        try {
            Statement statement = connection.createStatement();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

//    建立数据库连接
    public static Connection connection() {
        Connection connection = null;   // 先在外部创建变量，在局部（try内）创建变量不能在外部使用
        try {
            connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

//    public static PreparedStatement statement(Connection connection) {
//        PreparedStatement preparedStatement = null;
//        try {
//            preparedStatement = connection.
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }


//    关闭增删改查资源
    public static void queryClose(Connection connection , Statement statement , ResultSet resultSet) {
        try {
            if (connection != null) {
                connection.close();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            connection = null;
        }

        try {
            if (statement != null) {
                statement.close();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            statement = null;
        }

        try {
            if (resultSet != null) {
                resultSet.close();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            resultSet = null;
        }
    }

//    关闭增删改资源
    public static void close(Connection connection , Statement statement) {
        try {
            if (connection != null) {
                connection.close();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            connection = null;
        }
        try {
            if (statement != null) {
                statement.close();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            statement = null;
        }

    }
}
